package com.sfang.common.base.utils;

import cn.hutool.core.collection.CollUtil;
import com.google.common.collect.Lists;
import com.sfang.common.base.vo.TreeVO;

import java.util.List;

/**
 * @author Java丶小明
 * @date 2023/12/5 10:18
 */
public class CommonUtil {
    public static <T extends TreeVO> List<T> getFunctionMenuVOList(List<T> list) {
        if(CollUtil.isEmpty(list)){
            return list;
        }
        List<T> parentMenuList = Lists.newArrayList();
        for (T parentMenu : list) {
            if (parentMenu.getParentId().equals(-1)) {
                parentMenuList.add(parentMenu);
            }
        }
        // 4、遍历出父菜单对应的子菜单
        for (T parent : parentMenuList) {
            List<T> child = CommonUtil.getChildMenu(parent.getId(), list);
            parent.setChildren(child);
        }
        return parentMenuList;
    }

    public static  <T extends TreeVO> List<T> getChildMenu(Integer parentMenuId, List<T> allMenuList) {
        // 5、存放子菜单的集合
        List<T> childMenuList = Lists.newArrayList();
        for (T menu : allMenuList) {
            if (menu.getParentId().equals(parentMenuId)) {
                childMenuList.add(menu);
            }
        }
        // 6、递归
        for (T treeVO : childMenuList) {
            treeVO.setChildren(CommonUtil.getChildMenu(treeVO.getId(), allMenuList));
        }
        return childMenuList;
    }
}
